package com.dronff.gvm.utils;

import com.dronff.gvm.entity.Klass;
import com.dronff.gvm.interfaces.Allocatable;
import com.dronff.gvm.interfaces.WriteFunction;
import com.dronff.gvm.memorymanager.SafeBytes;
import com.dronff.gvm.memorymanager.impl.MiniMemoryImpl;

import java.nio.charset.StandardCharsets;

/**
 * @author: tyf
 * @date: 2022/9/9 10:40
 * @description: todo(
 */
@Deprecated
public class MemoryAllocUtils {
//    public MiniMemoryImpl newObject(Allocatable allocatable, Object object){
//
//    }
    public static MiniMemoryImpl newKlass(Allocatable allocatable, Klass klass) throws IllegalAccessException {
        byte[] bytes = klass.getName().getBytes(StandardCharsets.UTF_8);
        MiniMemoryImpl alloc = allocatable.alloc(bytes.length+2);
        alloc.writeOnly(new WriteFunction<SafeBytes>() {
            @Override
            public void write(SafeBytes data) {
                for(int i=0;i<bytes.length;i++){
                    data.appendByte(bytes[i]);
                }
            }
        });
        return alloc;
    }
}
